package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.server.HttpServerResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.IBasicEnum;
import com.zzyl.base.PageResponse;
import com.zzyl.constant.StatusConstant;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.NursingPlanExistNameException;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.service.NursingProjectPlanService;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanService nursingProjectPlanService;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询所有护理计划
     * @return
     */
    @Override
    public List<NursingPlan> findAll() {
        // 调用Mapper层,查询所有护理计划
        List<NursingPlan> nursingPlanList = nursingPlanMapper.findAll();
        // 返回数据
        return nursingPlanList;
    }

    /**
     * 删除护理计划
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        // 拿着id去数据库查询,看是否被调用,如果正在被调用,就抛出异常,提示正在被调用无法删除
        // int selectCount = nursingProjectPlanService.findIdJob(id);
        // if (selectCount != 0) {
        //     // 返回异常,提示:该项目已被使用,无法删除
        //     throw new RuntimeException("该项目已被使用,无法删除");
        // }
        // 调用mapper层根据id删除护理计划
        nursingPlanMapper.deleteById(id);
        // 调用关联表业务层,根据id删除关联表数据
        nursingProjectPlanService.deleteByIdNpp(id);
    }

    /**
     * 护理计划条件分页查询
     *
     * @param name
     * @param status
     * @param pageSize
     * @param pageNum
     * @return
     */
    @Override
    public PageResponse<NursingPlanVo> searchNursingPlan(String name, Integer status, Integer pageSize, Integer pageNum) {
        // 分页插件
        PageHelper.startPage(pageNum, pageSize);
        // 调用Mapper层,护理计划条件分页查询
        Page<NursingPlanVo> page = nursingPlanMapper.searchNursingPlan(name, status, pageNum, pageSize);


        // 返回
        PageResponse<NursingPlanVo> pageResponse = new PageResponse<>(page);
        pageResponse.setRecords(page.getResult());

        return pageResponse;

    }

    /**
     * 新增护理计划
     * @param nursingPlanDto
     */
    @Transactional
    @Override
    public void save(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        // 默认禁用
        nursingPlan.setStatus(StatusConstant.DISABLE);

        // 往nursing_plan中添加插入数据
        try {
            nursingPlanMapper.save(nursingPlan);
        } catch (Exception e) {
            throw new NursingPlanExistNameException(BasicEnum.NURSING_PLAN_EXIST_NAME);
        }
        Long id = nursingPlan.getId();


        List<NursingProjectPlan> nursingProjectPlans = new ArrayList<>();

        for (NursingProjectPlanDto projectPlan : nursingPlanDto.getProjectPlans()) {
            NursingProjectPlan nursingProjectPlan = BeanUtil.toBean(projectPlan, NursingProjectPlan.class);
            nursingProjectPlans.add(nursingProjectPlan);
        }

        // 遍历护理计划里的项目分别设置计划id
        for (NursingProjectPlan nursingProjectPlan : nursingProjectPlans) {
            nursingProjectPlan.setPlanId(id);
        }

        // 往nursing_project_plan批量插入数据
        nursingProjectPlanMapper.saveBatch(nursingProjectPlans);
    }

    /**
     * 修改护理计划
     * @param nursingPlanDto
     */
    @Override
    public void update(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);

        // 修改nursing_plan中的数据
        try {
            nursingPlanMapper.update(nursingPlan);
        } catch (Exception e) {
            throw new NursingPlanExistNameException(BasicEnum.NURSING_PLAN_EXIST_NAME);
        }

        // 删除护理计划原本关联的护理项目
        nursingProjectPlanMapper.deleteByIdNpp(nursingPlanDto.getId());

        List<NursingProjectPlan> nursingProjectPlans = new ArrayList<>();

        for (NursingProjectPlanDto projectPlan : nursingPlanDto.getProjectPlans()) {
            NursingProjectPlan nursingProjectPlan = BeanUtil.toBean(projectPlan, NursingProjectPlan.class);
            nursingProjectPlans.add(nursingProjectPlan);
        }

        // 遍历护理计划里的项目分别设置计划id
        for (NursingProjectPlan nursingProjectPlan : nursingProjectPlans) {
            nursingProjectPlan.setPlanId(nursingPlan.getId());
        }

        // 往nursing_project_plan批量插入数据
        nursingProjectPlanMapper.saveBatch(nursingProjectPlans);
    }

    /**
     * 根据id查询护理计划
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo getById(Long id) {
        // 调用Mapper层,根据id查询护理数据
        NursingPlanVo nursingPlanVo = nursingPlanMapper.getById(id);
        // 复制
        return nursingPlanVo;
    }

    /**
     * 护理计划启用禁用
     * @param id
     * @param status
     */
    @Override
    public void onOff(Long id, Integer status) {
        NursingPlan nursingPlan = new NursingPlan();
        nursingPlan.setId(id);
        nursingPlan.setStatus(status);

        // 调用Mapper层,启用禁用
        nursingPlanMapper.update(nursingPlan);
    }


}
